package vn.student;

import vn.student.controller.ViewContactDetailController;
import vn.student.mylib.Contact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

	/*
	
		Class name: ViewContactDetail.java
		Function: Day la lop ke thua lop Activity, la lop khoi tao cua so Activity dung de xem thong tin chi tiet mot Contact.
		
	*/

public class ViewContactDetail extends Activity implements View.OnClickListener{
	String id = null;
	boolean isEdit = false;
	boolean isInstructor = false;
	boolean isCreateNew = false;
	ViewContactDetailController viewContactDetail = null;
	/*
		Method name: public void onCreate(Bundle savedInstanceState).
		Function: Duoc chay dau tien khi Activity duoc mo len. Phuong thuc khoi tao doi tuong controller va lay thong tin chi tiet ve 
			lop mon hoc va hien thi len GUI.
		Parameter: Bundle savedInstanceState: Nhung thong tin duoc luu lai truoc khi Activity 
			chuyen tu trang thai Running qua Plaused.
		Return: None.
		Note:
    */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_contact_detail);
		id =this.getIntent().getExtras().getString("id");
		isEdit = this.getIntent().getExtras().getBoolean("isEdit");
		isInstructor = this.getIntent().getExtras().getBoolean("isInstructor");
		isCreateNew = this.getIntent().getExtras().getBoolean("isCreateNew");
		if(isEdit){
			this.changeTextViewToEditState();
		}
		else
			this.hideEditButtons();
		this.setOnClickEditButtonsListener(); 
		// Khoi tao doi tuong controller.
		viewContactDetail = new ViewContactDetailController(this,id,isInstructor,isCreateNew);
		viewContactDetail.viewInstructorContactDetail();
		RelativeLayout tmp = (RelativeLayout) findViewById(R.id.button01);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!isEdit)
					onClickBackButton();
				else
					onClickCancelButton();
					
			}
		});
		tmp = (RelativeLayout) findViewById(R.id.button02);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!isEdit)
					onClickEditButton();
				else
					onClickSaveButton();
			}
		});
		if(isInstructor)
			this.showClassList();
		else{
			TextView txtView = (TextView) this.findViewById(R.id.tv02);
			txtView.setText("Student");
			this.showAssignmentList();
		}
	}
	/*
		Method name: private void showClassList().
		Function: Hien thi danh sach lop mon hoc ma giang vien giang day.
		Parameter: None.
		Return: None.
		Note:
    */
	private void showClassList(){
		ListView listView = (ListView)this.findViewById(R.id.classListView);
		if(id!=null)
			viewContactDetail.getClassListView(listView, id);
	}
	/*
		Method name: private void showAssignmentList().
		Function: Hien thi danh sach bai tap ma contact tham gia nhu la partner.
		Parameter: None.
		Return: None.
		Note:
    */
	private void showAssignmentList(){
		ListView listView = (ListView)this.findViewById(R.id.classListView);
		if(id!=null)
			viewContactDetail.getAssignmentListView(listView, id);
	}
	/*
		Method name: protected void onActivityResult(int requestCode, int resultCode,
	                                   Intent data).
		Function: Override phuong thuc onActivityResult() de su ly su kien cac Activity con cua Activity hien tai bi close.
		Parameter: int requestCode: Ma code cua activity da goi Activity vua close, int resultCode: ket qua Activity tra ve, 
			Intent data: Doi tuong Intent chua thong tin duoc tra ve tu Activity moi duoc close.
		Return: None.
		Note:
    */
	@Override
	protected void onActivityResult(int requestCode, int resultCode,
	                                   Intent data) {
	    if (requestCode==0) {
	      if (resultCode==RESULT_OK) {
	    	  String title = data.getExtras().getString("title");
	    	  String newValue = data.getExtras().getString("newValue");
	    	  TextView label;
	    	  int id = 0;
	    	  if(title.equals("contactName"))
	    		  id = R.id.contactNameLabel;
	    	  else if(title.equals("email"))
	    		  id = R.id.emailLabel;
	    	  else if(title.equals("phone"))
	    		  id = R.id.phoneNoLabel;
	    	  else if(title.equals("add"))
	    		  id = R.id.addLabel;
	    	  else if(title.equals("note"))
	    		  id = R.id.noteLabel;
	    	  else if(title.equals("web"))
	    		  id = R.id.webLabel;
		      if(id!=0){
		    	  label = (TextView) findViewById(id);
		    	  label.setText(newValue);
		      }
	      }
	    }
	  }
	/*
		Method name: private void setOnClickEditButtonsListener().
		Function: Gan doi tuong Listener de su ly su kien nhan nut edit cac thong tin.
		Parameter: None.
		Return: None.
		Note:
    */
	private void setOnClickEditButtonsListener(){
		
		RelativeLayout tmp = (RelativeLayout) findViewById(R.id.editEmailBtn);
		tmp.setOnClickListener(this);
		tmp = (RelativeLayout) findViewById(R.id.editContactNameBtn);
		tmp.setOnClickListener(this);
		tmp = (RelativeLayout) findViewById(R.id.editPhoneBtn);
		tmp.setOnClickListener(this);
		tmp = (RelativeLayout) findViewById(R.id.editAddBtn);
		tmp.setOnClickListener(this);
		tmp = (RelativeLayout) findViewById(R.id.editWebBtn);
		tmp.setOnClickListener(this);
		tmp = (RelativeLayout) findViewById(R.id.editNoteBtn);
		tmp.setOnClickListener(this);
		//showEditButtons();
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(this,ViewInputTextBox.class);
		Bundle bundle = new Bundle();
		//bundle.putString("newValue","");
		TextView label = null;
		if(!isEdit) return;
		switch(arg0.getId()){
		case R.id.editContactNameBtn:
			bundle.putString("title", "contactName");
			label = (TextView) findViewById(R.id.contactNameLabel);
			bundle.putString("oldValue",(String) label.getText());
			intent.putExtras(bundle);
			startActivityForResult(intent,0);
			break;
		case R.id.editEmailBtn:
			bundle.putString("title", "email");
			label = (TextView) findViewById(R.id.emailLabel);
			bundle.putString("oldValue",(String) label.getText());
			intent.putExtras(bundle);
			startActivityForResult(intent,0);
			break;
		case R.id.editPhoneBtn:
			bundle.putString("title", "phone");
			label = (TextView) findViewById(R.id.phoneNoLabel);
			bundle.putString("oldValue",(String) label.getText());
			intent.putExtras(bundle);
			startActivityForResult(intent,0);
			break;
		case R.id.editAddBtn:
			bundle.putString("title", "add");
			label = (TextView) findViewById(R.id.addLabel);
			bundle.putString("oldValue",(String) label.getText());
			intent.putExtras(bundle);
			startActivityForResult(intent,0);
			break;
		case R.id.editNoteBtn:
			bundle.putString("title", "note");
			label = (TextView) findViewById(R.id.noteLabel);
			bundle.putString("oldValue",(String) label.getText());
			intent.putExtras(bundle);
			startActivityForResult(intent,0);
			break;
		case R.id.editWebBtn:
			bundle.putString("title", "web");
			label = (TextView) findViewById(R.id.webLabel);
			bundle.putString("oldValue",(String) label.getText());
			intent.putExtras(bundle);
			startActivityForResult(intent,0);
			break;
		}
	}
	/*
		Method name: private void showEditButtons().
		Function: Hien thi cac nut de edit thong tin contact.
		Parameter: None.
		Return: None.
		Note:
    */
	private void showEditButtons(){ 

		TextView txtView = null;
		txtView = (TextView)this.findViewById(R.id.editBtn01);
		txtView.setVisibility(View.VISIBLE);
		txtView = (TextView)this.findViewById(R.id.editBtn02);
		txtView.setVisibility(View.VISIBLE);
		txtView = (TextView)this.findViewById(R.id.editBtn03);
		txtView.setVisibility(View.VISIBLE);
		txtView = (TextView)this.findViewById(R.id.editBtn04);
		txtView.setVisibility(View.VISIBLE);
		txtView = (TextView)this.findViewById(R.id.editBtn05);
		txtView.setVisibility(View.VISIBLE);
		txtView = (TextView)this.findViewById(R.id.editBtn06);
		txtView.setVisibility(View.VISIBLE);
	}
	/*
		Method name: private void hideEditButtons().
		Function: An cac nut de edit thong tin contact.
		Parameter: None.
		Return: None.
		Note:
    */
	private void hideEditButtons(){ 

		TextView txtView = null;
		txtView = (TextView)this.findViewById(R.id.editBtn01);
		txtView.setVisibility(View.INVISIBLE);
		txtView = (TextView)this.findViewById(R.id.editBtn02);
		txtView.setVisibility(View.INVISIBLE);
		txtView = (TextView)this.findViewById(R.id.editBtn03);
		txtView.setVisibility(View.INVISIBLE);
		txtView = (TextView)this.findViewById(R.id.editBtn04);
		txtView.setVisibility(View.INVISIBLE);
		txtView = (TextView)this.findViewById(R.id.editBtn05);
		txtView.setVisibility(View.INVISIBLE);
		txtView = (TextView)this.findViewById(R.id.editBtn06);
		txtView.setVisibility(View.INVISIBLE);
	}
	/*
		Method name: public void onClickBackButton().
		Function: Close Activity hien tai de tra ve Activity truoc do.
		Parameter: None.
		Return: None.
		Note:
    */
	public void onClickBackButton(){
		this.setResult(Activity.RESULT_CANCELED);
		this.finish();
	}
	/*
		Method name: public void onClickEditButton().
		Function: Chuyen GUI sang trang thai edit.
		Parameter: None.
		Return: None.
		Note:
    */
	public void onClickEditButton(){
		isEdit = true;
		showEditButtons();
		this.changeTextViewToEditState();
		
	}
	/*
		Method name: public void onClickCancelButton().
		Function: Thoat khoi che do edit mode.
		Parameter: None.
		Return: None.
		Note:
    */
	public void onClickCancelButton(){
		isEdit = false;
		if(isCreateNew){
			this.setResult(Activity.RESULT_CANCELED);
			this.finish();
		}
		else{
			viewContactDetail.viewInstructorContactDetail();
			this.changeTextViewEditStateToView();
		}
	}
	/*
		Method name: public void onClickSaveButton().
		Function: Cap nhap thong tin contact moi chinh sua vao CSDL.
		Parameter: None.
		Return: None.
		Note:
    */
	public void onClickSaveButton(){
		isEdit = false;
		String info[] = new String[8];
		TextView tmp = (TextView) findViewById(R.id.contactNameLabel);
		info[0] = id;
		info[1] = (String) tmp.getText();
		tmp = (TextView) findViewById(R.id.phoneNoLabel);
		info[2] = (String) tmp.getText();
		tmp = (TextView) findViewById(R.id.emailLabel);
		info[3] = (String) tmp.getText();
		tmp = (TextView) findViewById(R.id.webLabel);
		info[4] = (String) tmp.getText();
		tmp = (TextView) findViewById(R.id.noteLabel);
		info[5] = (String) tmp.getText();
		info[6] = "photo";
		tmp = (TextView) findViewById(R.id.addLabel);
		info[7] = (String) tmp.getText();
		Contact contact = new Contact(info);
		if(isInstructor)
			contact.type = "TEACHER";
		else
			contact.type = "STUDENT";
		long id = viewContactDetail.updateContact(contact);
		if(isCreateNew){
			EditText inputTextBox = (EditText) findViewById(R.id.inputNewValueTexbox);
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			if(isInstructor){
				bundle.putString("instructorId",String.valueOf(id));
				bundle.putString("instructorName", contact.name);
			}
			else{
				bundle.putString("studentId",String.valueOf(id));
				bundle.putString("studentName", contact.name);
			}
			intent.putExtras(bundle);
			this.setResult(RESULT_OK, intent);
			this.finish();
		}
		//hideEditButtons();
		this.changeTextViewEditStateToView();
	}
	/*
		Method name: public void changeTextViewToEditState().
		Function:
		Parameter: None.
		Return: None.
		Note:
    */
	public void changeTextViewToEditState(){
		TextView txtView = (TextView)this.findViewById(R.id.tv01);
		txtView.setText("Cancel");
		txtView = (TextView)this.findViewById(R.id.tv03);
		txtView.setText("Save");
		showEditButtons();
	}
	/*
		Method name: public void changeTextViewEditStateToView().
		Function:
		Parameter: None.
		Return: None.
		Note:
    */
	public void changeTextViewEditStateToView(){
		TextView txtView = (TextView)this.findViewById(R.id.tv01);
		txtView.setText("Back");
		txtView = (TextView)this.findViewById(R.id.tv03);
		txtView.setText("Edit");
		hideEditButtons();
	}
}
